<?php
require "../classes/ContactCar.class.php";
require "../classes/CarDAO.class.php";
require "../classes/ConnectionFactory.class.php";
require "checksDate.action.php";

$date = new checksDate();
$car = new ContactCar();
$cf = new ConnectionFactory();
$db = $cf->getConnection();
$dao = new CarDAO($db);



$cadast = $date->dataAtual();

$id = '';
$car->setId($_POST['idCar']);
$car->setModel($_POST['carModelInput']);
$car->setMark($_POST['carMarkInput']);
$car->setColor($_POST['carColorInput']);
$car->setYear($_POST['carYearInput']);
$car->setPlaque($_POST['carLicenseInput']);
$car->setDateCad($cadast);

$id = $car->getId();



    
$plaque=$car->getPlaque();
$cadastrado=$dao->getByPlaque($plaque);
if (empty($id)) {
   if(!$cadastrado){
       $result = $dao->create($car);
      
   } else {$result=0;
           $result2=0;
   
   }
    
    
} else {
    $result= $dao->update($car);
    
}
header("Location: ../index.php");
?>
<script>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />;
    var cadTest = <?php echo $result;?>
     
           
    if(cadTest){
        alert("Cadastro Efetuado Com Sucesso!");
        setTimeout("document.location = '../index.php'",500);
       
    }else{
       
        alert("Não Foi Possível Realizar o Cadastro!");
        setTimeout("document.location = '../index.php'",500);
    }
    
             
</script>